package Stack;



//设计循环队列
class MyCircularQueue {
    public int front;
    public int rear;
    public int[] elem;

    public MyCircularQueue(int k) {
        elem = new int[k + 1];
    }

    public boolean enQueue(int value) {//入队列
        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }
    //出队列
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front =(front+1) %elem.length;
        return  true;
    }
    //得到 队头元素
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }
    //得到队尾元素
    public int Rear() {
        if(isEmpty()){
            return  -1;
        }
        int index = (rear==0)?elem.length-1:rear-1;
        return elem[index];
    }
    public boolean isEmpty() {
        return front==rear;
    }
    public boolean isFull() {
        return (rear+1)%elem.length==front;
    }
}
public class MyQueue {
    static  class  ListNode{
        public  int val;
        public ListNode prev;
        public  ListNode next;
        public ListNode (int val){
            this .val = val;
        }
    }
    public  ListNode first =null;
    public  ListNode last = null;
    public  int usedSize =0;
    public  void offer(int val){
        ListNode node =new ListNode(val);
        if(isEmpty()){
            first=last=node;
        }else {
            last.next =node;
            node.prev =null;
            last =last.next;
        }
        usedSize++;

    }
    public  int poll(){
        if(isEmpty()){
            return -1;
        }
        int val = first.val;
        first =first.next;
        if(first !=null){
            first.prev =null;
        }
        usedSize--;
        return val;
    }
    public  int peek() {
        if(isEmpty()){
            return  -1;
        }
        return  first.val;
    }
    public  boolean isEmpty(){
        return usedSize==0;
    }
}
